//package cn.com.spic.demo.controller;
//
//import cn.com.spic.demo.dto.R;
//import com.primeton.iam.client.UserDetailsUserInfo;
//import com.primeton.ptp.infra.data.UserInfo;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.oauth2.provider.OAuth2Authentication;
//import org.springframework.security.oauth2.provider.authentication.TokenExtractor;
//import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.HashMap;
//import java.util.Objects;
//
///**
// * 用户控制器
// *
// * @author lipangeng, Email:lipg@outlook.com
// * @version v1.0.0
// * @since v1.0.0
// * Created on 2020/11/4 4:04 下午
// */
//@RequestMapping("/backend/user")
//@RestController
//public class UserController {
//    @Autowired
//    private ResourceServerTokenServices tokenServices;
//    @Autowired
//    private TokenExtractor tokenExtractor;
//
//    /**
//     * 获取个人信息
//     *
//     * @since v1.0.0
//     * Created by lipangeng. Email:lipg@outlook.com
//     * Created on 2020/11/4 4:08 下午
//     */
//    @GetMapping("me")
//    public R<UserInfo> me(HttpServletRequest request) {
//        Authentication token = tokenExtractor.extract(request);
//        OAuth2Authentication authentication=null;
//        if (Objects.nonNull(token)){
//            authentication = tokenServices.loadAuthentication(token.getPrincipal().toString());
//        }
//        OAuth2Authentication finalAuthentication = authentication;
//        // 未登陆返回未登录提示
//        if (Objects.nonNull(finalAuthentication) && finalAuthentication.isAuthenticated()) {
//            Object principal = authentication.getPrincipal();
//            if (UserDetailsUserInfo.class.isAssignableFrom(principal.getClass())) {
//                UserDetailsUserInfo userDetailsUserInfo = (UserDetailsUserInfo) principal;
//                UserInfo userInfo = new UserInfo();
//                userInfo.setUsername(userDetailsUserInfo.getUsername());
//                userInfo.setNickname(userDetailsUserInfo.getNickname());
//                userInfo.setEmail(userDetailsUserInfo.getEmail());
//                userInfo.setAttributes(userDetailsUserInfo.getAttributes());
//                return R.ofSuccess(userInfo);
//            }
//        }
//        return R.ofError("403", "未登录", null);
//    }
//
//    /**
//     * 是否已经登陆
//     *
//     * @since v1.0.0
//     * Created by lipangeng. Email:lipg@outlook.com
//     * Created on 2020/11/4 4:13 下午
//     */
//    @GetMapping("logged")
//    public R<?> logged(HttpServletRequest request) {
//        Authentication token = tokenExtractor.extract(request);
//        OAuth2Authentication authentication=null;
//        if (Objects.nonNull(token)){
//             authentication = tokenServices.loadAuthentication(token.getPrincipal().toString());
//        }
//        OAuth2Authentication finalAuthentication = authentication;
//        return R.ofSuccess(new HashMap<String, Boolean>() {{
//            put("logged", Objects.nonNull(finalAuthentication) && finalAuthentication.isAuthenticated());
//        }});
//    }
//}
